package com.cheyun.netsalev3.repository.orderquanyi

import androidx.paging.PageKeyedDataSource
import com.cheyun.netsalev3.bean.orderquanyi.OrderQuanyiChildData
import com.cheyun.netsalev3.bean.orderquanyi.OrderQuanyiChildParam
import com.cheyun.netsalev3.repository.BasePageRepository
import com.cheyun.netsalev3.utils.api.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class OrderQuanyiChildRepository(val orderId:String,val nkey: String, var map: Map<String, String> = HashMap(), var boolean: Boolean = false, var enable:Boolean = false) : BasePageRepository<OrderQuanyiChildData>() {
    fun changeMap(newMap:HashMap<String, String>){
        this.map = newMap
    }
    fun setEanbleLoad(e:Boolean){
        this.enable = e
    }
    override fun loadInitial(params: PageKeyedDataSource.LoadInitialParams<Int>, callback: (data: MutableList<OrderQuanyiChildData>) -> Unit) {
        if(!this.enable)return
        ApiClient.getInstance().getOrderQuanyiAllList(1,orderId,nkey, map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<OrderQuanyiChildParam>>() {
                    override fun onSuccess(t: BaseResp<OrderQuanyiChildParam>) {
//                        t.data = AfterSale.getMaintenanceData()
                        t.data?.rows?.apply {
                            total.postValue(t.data!!.total)

                            callback(this)
                        }
                    }

                    override fun onError(ex: ApiException) {
                        networkState.postValue(NetworkState.error(ex.getMessage()))
                    }
                })
    }


    override fun loadAfter(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, OrderQuanyiChildData>) {
        if(!this.enable)return
        ApiClient.getInstance().getOrderQuanyiAllList(params.key,orderId,nkey, map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<OrderQuanyiChildParam>>() {
                    override fun onSuccess(t: BaseResp<OrderQuanyiChildParam>) {
//                        t.data = AfterSale.getMaintenanceData()

                        t.data?.rows?.apply {

                            callback.onResult(this, params.key + 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

    override fun loadBefore(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, OrderQuanyiChildData>) {
        if(!this.enable)return
        ApiClient.getInstance().getOrderQuanyiAllList(params.key,orderId,nkey, map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<OrderQuanyiChildParam>>() {
                    override fun onSuccess(t: BaseResp<OrderQuanyiChildParam>) {
//                        t.data = AfterSale.getMaintenanceData()
                        t.data?.rows?.apply {

                            callback.onResult(this, params.key - 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

}